package de.rj.urwalking.ui.fragments.dialog;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.threed.jpct.Object3D;

import de.rj.urwalking.R;
import de.rj.urwalking.data.Pin;
import de.rj.urwalking.db.DBSalientObject;
import de.rj.urwalking.ui.activities.QuestionsActivity;

/**
 * 
 * @author rjj
 * 
 */
public class IndoorObjectDialogFragment extends LandmarkDialogFragment implements OnClickListener {

	private Object3D object3d;
	private boolean removePin = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = super.onCreateView(inflater, container, savedInstanceState);

		showObjectName();
		hideInfoView();

		return view;
	}

	private void showObjectName() {
		object3d = (Object3D) getArguments().getSerializable("object");

		String objectName = object3d.getName();

		// clean name for jpct's loaded objs
		int underscoreIndex = objectName.indexOf("_");
		if (underscoreIndex != -1) {
			objectName = objectName.substring(0, underscoreIndex);
		}

		if (object3d instanceof Pin) {
			objectName = "Neue Landmarke";
		}

		getDialog().setTitle(objectName);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.suggestButton:

			ensureExperiment();
			Intent intent = new Intent(getActivity(), QuestionsActivity.class);

			intent.putExtra("object",
					new DBSalientObject(Double.valueOf(object3d.getCenter().x), Double.valueOf(object3d.getCenter().y),
							true));

			startQuestions(intent);
			this.dismiss();
			break;
		case R.id.backButton:
			if (object3d instanceof Pin) {
				removePin = true;
			}
			this.dismiss();
			break;
		default:
			break;
		}

	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);

		if (removePin) {
			// remove the pin for the new object if the user changes his mind and cancels the dialog
			object3d.clearObject();
		}
	}
}
